namespace SinaFinanceBaseClassLibrary
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;

    public static class DB_Transition
    {
        private static string Cal_PercentLineYAxisTips(double value)
        {
            double num = Math.Pow((double) 1.0, (double) 2.0);
            bool flag = true;
            string str = string.Empty;
            if (value <= 0.0)
            {
                flag = false;
            }
            if (Math.Abs(value) >= 1.0)
            {
                return str;
            }
            if (value.ToString().Length > 5)
            {
                double num2;
                string str2 = (value.ToString().Length > 6) ? value.ToString().Substring(0, 6) : value.ToString();
                if (flag)
                {
                    num2 = (Convert.ToDouble(str2) * 100.0) + 0.01;
                    return num2.ToString();
                }
                num2 = (-Convert.ToDouble(str2) * 100.0) - 0.01;
                return num2.ToString();
            }
            return value.ToString();
        }

        public static void DB_InitDoubleTimeSharingPointsInfo(Dictionary<string, List<DB_MinLine>> dataSource, List<string> settleMents, out List<string> yaxisTips, out Dictionary<string, List<TimeSharingPoint>> pointDic)
        {
            yaxisTips = new List<string>();
            pointDic = new Dictionary<string, List<TimeSharingPoint>>();
            if (dataSource.Count.Equals(settleMents.Count))
            {
                double num = 0.0;
                IEnumerator enumerator = (IEnumerator) dataSource.Keys.GetEnumerator();
                IEnumerator enumerator2 = (IEnumerator) settleMents.GetEnumerator();
                while (enumerator.MoveNext() && enumerator2.MoveNext())
                {
                    double num2 = Convert.ToDouble(enumerator2.Current.ToString());
                    List<TimeSharingPoint> list = new List<TimeSharingPoint>();
                    string str2 = enumerator.Current.ToString();
                    List<DB_MinLine> list2 = dataSource[str2];
                    if ((list2 != null) && (list2.Count > 0))
                    {
                        list2.RemoveAt(0);
                    }
                    IEnumerator enumerator3 = (IEnumerator) list2.GetEnumerator();
                    while (enumerator3.MoveNext())
                    {
                        DB_MinLine line = (DB_MinLine) enumerator3.Current;
                        double num4 = (Convert.ToDouble(line.price) - num2) / num2;
                        if (Math.Abs(num4) >= Math.Abs(num))
                        {
                            num = num4;
                        }
                        string volume = line.volume;
                        string mintime = line.mintime;
                        string str5 = num4.ToString();
                        TimeSharingPoint point = new TimeSharingPoint {
                            XInfo = mintime,
                            YInfo = str5,
                            VolumnInfo = volume,
                            StockId = str2,
                            PointType = PointTypeEnum.Percent
                        };
                        list.Add(point);
                    }
                    pointDic.Add(str2, list);
                }
                string str6 = Cal_PercentLineYAxisTips(Math.Abs(num)) + "%";
                string str7 = Cal_PercentLineYAxisTips((Math.Abs(num) * 3.0) / 4.0) + "%";
                string str8 = Cal_PercentLineYAxisTips(Math.Abs(num) / 2.0) + "%";
                string str9 = Cal_PercentLineYAxisTips(Math.Abs(num) / 4.0) + "%";
                string str10 = "0.00%";
                string str11 = "-" + str9;
                string str12 = "-" + str8;
                string str13 = "-" + str7;
                string str14 = "-" + str6;
                yaxisTips.Add(str6);
                yaxisTips.Add(str7);
                yaxisTips.Add(str8);
                yaxisTips.Add(str9);
                yaxisTips.Add(str10);
                yaxisTips.Add(str11);
                yaxisTips.Add(str12);
                yaxisTips.Add(str13);
                yaxisTips.Add(str14);
            }
        }

        public static void DB_InitSingleTimeSharingPointsInfo(List<DB_MinLine> dataSource, List<string> settleMents, out List<TimeSharingPoint> timeSharingPoint, out List<string> leftYaxisTips, out List<TimeSharingPoint> averagePoints, out List<string> rightYaxisTips, out List<string> volumnYAxisTips)
        {
            timeSharingPoint = new List<TimeSharingPoint>();
            averagePoints = new List<TimeSharingPoint>();
            leftYaxisTips = new List<string>();
            rightYaxisTips = new List<string>();
            volumnYAxisTips = new List<string>();
            double num = 0.0;
            double num2 = 0.0;
            double num3 = 0.0;
            double num4 = 0.0;
            double num5 = 0.0;
            double num6 = 0.0;
            double num7 = 0.0;
            if (((settleMents != null) && settleMents.Count.Equals(1)) && ((dataSource != null) && (dataSource.Count > 0)))
            {
                double num8 = double.Parse(settleMents[0]);
                num = num2 = num3 = double.Parse(settleMents[0]);
                num7 = double.Parse(dataSource[0].volume);
                dataSource.RemoveAt(0);
                foreach (DB_MinLine line in dataSource)
                {
                    double num9 = double.Parse(line.price);
                    double num10 = (double.Parse(line.price) - num8) / num8;
                    double num11 = double.Parse(line.volume);
                    num = (num < num9) ? num9 : num;
                    num2 = (num2 > num9) ? num9 : num2;
                    num4 = (num4 < num10) ? num10 : num4;
                    num5 = (num5 > num10) ? num10 : num5;
                    num7 = (num7 < num11) ? num11 : num7;
                    timeSharingPoint.Add(new TimeSharingPoint { XInfo = line.mintime, YInfo = line.price, VolumnInfo = line.volume, StockName = line.symbol, StockId = line.symbol, PointType = PointTypeEnum.TimeSharing });
                    averagePoints.Add(new TimeSharingPoint { XInfo = line.mintime, YInfo = line.avg_price, VolumnInfo = line.volume, StockName = line.symbol, StockId = line.symbol, PointType = PointTypeEnum.Average });
                }
                double num12 = num7 / 4.0;
                for (int i = 0; i < 5; i++)
                {
                    volumnYAxisTips.Add((num7 - (i * num12)).ToString("0."));
                }
                double num14 = (Math.Abs((double) (Math.Abs(num) - num3)) >= Math.Abs((double) (num3 - Math.Abs(num2)))) ? Math.Abs((double) (Math.Abs(num) - num3)) : Math.Abs((double) (num3 - Math.Abs(num2)));
                num = num3 + num14;
                num2 = num3 - num14;
                if (num14 >= 0.1)
                {
                    leftYaxisTips.Add(Hold2BitPlace(num));
                    leftYaxisTips.Add(Hold2BitPlace(num - ((num - num3) / 4.0)));
                    leftYaxisTips.Add(Hold2BitPlace(num - (2.0 * ((num - num3) / 4.0))));
                    leftYaxisTips.Add(Hold2BitPlace(num - (3.0 * ((num - num3) / 4.0))));
                    leftYaxisTips.Add(Hold2BitPlace(num3));
                    leftYaxisTips.Add(Hold2BitPlace(num3 - ((num3 - num2) / 4.0)));
                    leftYaxisTips.Add(Hold2BitPlace(num3 - (2.0 * ((num3 - num2) / 4.0))));
                    leftYaxisTips.Add(Hold2BitPlace(num3 - (3.0 * ((num3 - num2) / 4.0))));
                    leftYaxisTips.Add(Hold2BitPlace(num2));
                }
                else
                {
                    leftYaxisTips.Add(Hold3BitPlace(num));
                    leftYaxisTips.Add(Hold3BitPlace(num - ((num - num3) / 4.0)));
                    leftYaxisTips.Add(Hold3BitPlace(num - (2.0 * ((num - num3) / 4.0))));
                    leftYaxisTips.Add(Hold3BitPlace(num - (3.0 * ((num - num3) / 4.0))));
                    leftYaxisTips.Add(Hold3BitPlace(num3));
                    leftYaxisTips.Add(Hold3BitPlace(num3 - ((num3 - num2) / 4.0)));
                    leftYaxisTips.Add(Hold3BitPlace(num3 - (2.0 * ((num3 - num2) / 4.0))));
                    leftYaxisTips.Add(Hold3BitPlace(num3 - (3.0 * ((num3 - num2) / 4.0))));
                    leftYaxisTips.Add(Hold3BitPlace(num2));
                }
                double num15 = (Math.Abs((double) (Math.Abs(num4) - num6)) >= Math.Abs((double) (num6 - Math.Abs(num5)))) ? Math.Abs((double) (Math.Abs(num4) - num6)) : Math.Abs((double) (num6 - Math.Abs(num5)));
                num4 = num6 + num15;
                num5 = num6 - num15;
                if (num15 >= 0.1)
                {
                    rightYaxisTips.Add(HoldPlaceAndPlus(num4 * 100.0) + "%");
                    rightYaxisTips.Add(HoldPlaceAndPlus((num4 - ((num4 - num6) / 4.0)) * 100.0) + "%");
                    rightYaxisTips.Add(HoldPlaceAndPlus((num4 - (2.0 * ((num4 - num6) / 4.0))) * 100.0) + "%");
                    rightYaxisTips.Add(HoldPlaceAndPlus((num4 - (3.0 * ((num4 - num6) / 4.0))) * 100.0) + "%");
                    rightYaxisTips.Add(HoldPlaceAndPlus(num6 * 100.0) + "%");
                    rightYaxisTips.Add(HoldPlaceAndPlus((num6 - ((num6 - num5) / 4.0)) * 100.0) + "%");
                    rightYaxisTips.Add(HoldPlaceAndPlus((num6 - (2.0 * ((num6 - num5) / 4.0))) * 100.0) + "%");
                    rightYaxisTips.Add(HoldPlaceAndPlus((num6 - (3.0 * ((num6 - num5) / 4.0))) * 100.0) + "%");
                    rightYaxisTips.Add(HoldPlaceAndPlus(num5 * 100.0) + "%");
                }
                else
                {
                    rightYaxisTips.Add(Hold3BitPlaceAndPlus(num4 * 100.0) + "%");
                    rightYaxisTips.Add(Hold3BitPlaceAndPlus((num4 - ((num4 - num6) / 4.0)) * 100.0) + "%");
                    rightYaxisTips.Add(Hold3BitPlaceAndPlus((num4 - (2.0 * ((num4 - num6) / 4.0))) * 100.0) + "%");
                    rightYaxisTips.Add(Hold3BitPlaceAndPlus((num4 - (3.0 * ((num4 - num6) / 4.0))) * 100.0) + "%");
                    rightYaxisTips.Add(Hold3BitPlaceAndPlus(num6 * 100.0) + "%");
                    rightYaxisTips.Add(Hold3BitPlaceAndPlus((num6 - ((num6 - num5) / 4.0)) * 100.0) + "%");
                    rightYaxisTips.Add(Hold3BitPlaceAndPlus((num6 - (2.0 * ((num6 - num5) / 4.0))) * 100.0) + "%");
                    rightYaxisTips.Add(Hold3BitPlaceAndPlus((num6 - (3.0 * ((num6 - num5) / 4.0))) * 100.0) + "%");
                    rightYaxisTips.Add(Hold3BitPlaceAndPlus(num5 * 100.0) + "%");
                }
            }
        }

        public static void DB_KDay(DB_KLine _KLine, out List<KPoint> _KPoint, out List<string> _Day, out List<string> _KponitY, out List<string> _volumeY)
        {
            _KPoint = new List<KPoint>();
            _Day = new List<string>();
            _KponitY = new List<string>();
            _volumeY = new List<string>();
            double num = 0.0;
            double num2 = 0.0;
            double num3 = 0.0;
            if (((_KLine != null) && (_KLine.Data != null)) && (_KLine.Data.Count > 0))
            {
                int num5;
                List<DB_KLineData> list = _KLine.Data;
                num = double.Parse(list[0].High);
                num2 = double.Parse(list[0].Low);
                num3 = double.Parse(list[0].Volume);
                foreach (DB_KLineData data in list)
                {
                    num = (double.Parse(data.High) > num) ? double.Parse(data.High) : num;
                    num2 = (double.Parse(data.Low) < num2) ? double.Parse(data.Low) : num2;
                    num3 = (double.Parse(data.Volume) > num3) ? double.Parse(data.Volume) : num3;
                    _Day.Add(data.Day);
                    _KPoint.Add(new KPoint { XInfo = data.Day, VolumnInfo = data.Volume, StockName = _KLine.name, StockID = _KLine.symbol, DayStartPrice = data.Open, DayEndPrice = data.Close, DayPriceMax = data.High, DayPriceMin = data.Low });
                }
                double num4 = (num - num2) / 10.0;
                for (num5 = 0; num5 < 11; num5++)
                {
                    double num7 = num - (num5 * num4);
                    _KponitY.Add(num7.ToString("0.000"));
                }
                double num6 = num3 / 5.0;
                for (num5 = 0; num5 <= 5; num5++)
                {
                    _volumeY.Add((num3 - (num5 * num6)).ToString("0."));
                }
            }
        }

        private static string Hold2BitPlace(double _double)
        {
            return _double.ToString("0.00");
        }

        private static string Hold3BitPlace(double _double)
        {
            return _double.ToString("0.000");
        }

        private static string Hold3BitPlaceAndPlus(double _double)
        {
            double num = double.Parse(_double.ToString("0.000"));
            if (num <= 0.0)
            {
                return num.ToString("0.000");
            }
            return ("+" + num.ToString("0.000"));
        }

        private static string HoldPlaceAndPlus(double _double)
        {
            double num = double.Parse(_double.ToString("0.00"));
            if (num <= 0.0)
            {
                return num.ToString("0.00");
            }
            return ("+" + num.ToString("0.00"));
        }
    }
}
